#include "fxcc/core/graph/opengl3/ImGuiGlfwApp.h"
#include "fxcc/core/graph/common/Geometry.h"
#include "fxcc/core/graph/opengl3/TextureCubeJack.h"

#include "fxcc/imgui/Editor.h"
#include "fxcc/core/AssertManager.h"

using namespace fxcc::platform;
using namespace fxcc::graph;
using namespace fxcc::graph::opengl3;
using namespace fxcc::imgui::graph::opengl3;
using BaseApp = fxcc::imgui::graph::opengl3::GlfwApp;

#undef main

struct SimpleApp : public BaseApp
{
public:
    std::shared_ptr<fxcc::graph::opengl3::RenderTargetJack> m_RenderTarget;

    fxcc::graph::common::Transform transform;
    glm::vec4 color;
    std::shared_ptr<fxcc::graph::opengl3::Effect3d> m_Effect;
    std::shared_ptr<fxcc::graph::opengl3::MeshJack> m_CubeMesh;
    std::shared_ptr<TextureCubeJack> m_TextureCubeJack;

    fxcc::graph::common::Camera camera;

    SimpleApp(const fxcc::platform::common::App::Desc& desc) : BaseApp(desc), color(1.0)
    {
    }
    bool Init() override
    {
        if (!BaseApp::Init())
        {
            return false;
        }

        if (!InitResources())
        {
            return false;
        }

        return true;
    };

    void printOpenGLESVersion()
    {
        const char* version = (const char*)glGetString(GL_VERSION);
        std::cout << "OpenGL ES Version: " << version << std::endl;
    };

    bool InitResources()
    {
        auto& am = fxcc::AssertManager::Inst();
        std::string imagePath[6] = {
            am.GetFile("skybox/right.jpg"),
            am.GetFile("skybox/left.jpg"),
            am.GetFile("skybox/top.jpg"),
            am.GetFile("skybox/bottom.jpg"),
            am.GetFile("skybox/front.jpg"),
            am.GetFile("skybox/back.jpg") };

        std::string ddsPath = "Texture/desertcube1024.dds";

        TextureCubeJack::Desc desc;

        desc.m_WICImages[0] = am.GetAbsoluteFile("skybox/right.jpg");
        desc.m_WICImages[1] = am.GetAbsoluteFile("skybox/left.jpg");
        desc.m_WICImages[2] = am.GetAbsoluteFile("skybox/top.jpg");
        desc.m_WICImages[3] = am.GetAbsoluteFile("skybox/bottom.jpg");
        desc.m_WICImages[4] = am.GetAbsoluteFile("skybox/front.jpg");
        desc.m_WICImages[5] = am.GetAbsoluteFile("skybox/back.jpg");

        desc.m_HDRConfig.m_HDR = false;
        desc.m_HDRConfig.m_NrComponent = 4;

        desc.m_Type = TextureCubeJack::Desc::Type::_WIC_;


        // Create from dds
        desc.m_Type = TextureCubeJack::Desc::Type::_DDS_;
        desc.m_DDSPath = fxcc::AssertManager::GetAbsoluteFile(ddsPath);

        // Create from hdr
        desc.m_Type = TextureCubeJack::Desc::Type::_HDR_;
        desc.m_HDRPath = am.GetAbsoluteFile("hdr/qwantani_dusk_2_4k.hdr");
        desc.m_HDRConfig.m_Width = 1024;
        desc.m_HDRConfig.m_Height = 1024;
        desc.m_HDRConfig.m_HDR = 2;
        desc.m_HDRConfig.m_NrComponent = 4;

        //desc.m_HDRConfig.m_HDR = false;
        //desc.m_Type = TextureCubeJack::Desc::Type::_WIC_;
        //desc.m_Type = TextureCubeJack::Desc::Type::_HDR_;
        //desc.m_Type = TextureCubeJack::Desc::Type::_DDS_;
        desc.m_SamplerType = fxcc::graph::common::SamplerType::_sampler_2d_default_;
        m_TextureCubeJack = std::make_shared<TextureCubeJack>(desc);
        m_TextureCubeJack->Mipmap();

        m_TextureCubeJack->GenerateFaceTexture2D();
        //m_TextureCubeJack->CreateCubeDDS("output2/cubemap.dds");

        for (int i = 0; i < 6; i++)
        {
            auto face = m_TextureCubeJack->m_FaceTextures[i];
            face->SaveImage(std::string("face_") + std::to_string(i));

        }

        camera.SetPosition(glm::vec3(5.0f));
        camera.LookAt(glm::vec3(0.0f));
        const std::string fragCode = R"(
out vec4 FragColor;

uniform samplerCube environmentMap;

void main()
{
   vec3 envColor = textureLod(environmentMap, localPos, 0.0).rgb;
    
    // HDR tonemap and gamma correct
    // envColor = envColor / (envColor + vec3(1.0));
    // envColor = pow(envColor, vec3(1.0/2.2)); 
    
    FragColor = vec4(envColor, 1.0);
}
)";
        m_Effect = std::make_shared<fxcc::graph::opengl3::Effect3d>(fragCode);
        m_Effect->m_PipeLineType.m_RasterizerType = fxcc::graph::common::RasterizerType::_NONE_;

        m_CubeMesh = std::make_shared<fxcc::graph::opengl3::MeshJack>(fxcc::graph::common::Geometry::CreateBox());

        return true;
    };

    fxcc::imgui::Editor editor;

    void OnEdit() override
    {
        ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());

        ImGui::Begin("Hello world");
        editor.AddFrameBuffer(m_RenderTarget->m_Textures[0]->GetTextureId());
        ImGui::End();

        ImGui::Begin("Texture Cube");
        for (int i = 0; i < 6; i++)
        {
            const std::string& name = fxcc::graph::common::TextureCubeJack::CubeFaceName[i];

            auto tex = m_TextureCubeJack->m_FaceTextures[i];
            if (tex)
            {
                ImGui::Text(name.c_str());
                ImGui::Image(tex->GetTextureId(), { 200,200 });
            }
        }
        ImGui::End();

        
    }

    void OnDestroy() override
    {
        BaseApp::OnDestroy();
    }

    void OnFrameRender() override
    {
        glViewport(0, 0, m_Desc.m_Size.x, m_Desc.m_Size.y);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        //for (int i = 0; i < 6; i++)
        //{
        //    auto rt = m_FaceRT[i];
        //    rt->Begin();
        //    glClearColor(color.x, color.y, color.z, color.w);
        //    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        //    m_CubeFaceEffect->Begin();
        //    //m_CubeFaceEffect->SetCamera(camera);
        //    Effect3d::PassData passData;
        //    passData.m_Proj = fxcc::graph::common::TextureCubeJack::captureProjection;
        //    passData.m_View = fxcc::graph::common::TextureCubeJack::captureViews[i];
        //    passData.m_ProjView = passData.m_Proj * passData.m_View;

        //    m_CubeFaceEffect->m_Shader->Use();
        //    m_CubeFaceEffect->m_Shader->setInt("cubeMap", 0);
        //    m_CubeFaceEffect->m_Shader->setInt("gamma", m_TextureCubeJack->m_Desc.m_HDRConfig.m_HDR);
        //    m_TextureCubeJack->Active(0);

        //    m_CubeFaceEffect->SetPassData(passData);
        //    m_CubeFaceEffect->SetObjData(transform);
        //    m_CubeFaceEffect->InstMeshJack(m_CubeMesh.get());
        //    m_CubeFaceEffect->End();

        //    rt->End();
        //}

    };

    void OnFramebuffersize(int w, int h) override
    {
        opengl3::GlfwApp::OnFramebuffersize(w, h);
        camera.SetAspect(m_Desc.Aspect());

        m_RenderTarget = std::make_shared<fxcc::graph::opengl3::RenderTargetJack>(fxcc::graph::common::RenderTargetJack::Desc(1, { w,h, 4, 1 }));
    }
    void OnUpdate() override
    {
        opengl3::GlfwApp::OnUpdate();

        float ctTime = m_GlfwlTimer.m_CurrentTime;
        camera.FreeMove(m_Input, m_GlfwlTimer.m_DeltaTime);

        float deltaTime = m_GlfwlTimer.m_DeltaTime;
    }
};

int main()
{
    ztclog::pushFunction([](const ztclog::Line& line)
        {
            {
                std::cout << line.m_Text << std::endl;
            } });

            using _App = SimpleApp;
            using _Desc = fxcc::platform::common::App::Desc;

            _Desc desc;
            desc.m_Pos = glm::ivec2(100, 100);
            desc.m_Size = glm::ivec2(900, 600);
            desc.m_ScaleFactor = 1.0f;

            desc.m_Title = "test font";
            std::shared_ptr<_App> _app = std::make_shared<_App>(desc);
            return _app->Run();
};
